package com.spider.silence.worker;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.spider.silence.downloader.ChengDuWanBaoDownloader;
import com.spider.silence.downloader.SiChuanRenShiDownloader;
import com.spider.silence.output.FileWriter;
import com.spider.silence.output.FileWriterPool;
import com.spider.silence.pool.ChengDuWanBaoDownloaderPool;
import com.spider.silence.pool.SiChuanRenShiDownloaderPool;
import com.spider.silence.task.SiChuanRenShiTask;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.DateUtils;
import com.spider.silence.utils.MD5Utils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.Map;

/**
 * Created by Linbo Hu on 2017/7/5.
 * 四川人事考试网爬虫线程
 */
public class SiChuanRenShiWorker implements Runnable {

    private Logger logger = Logger.getLogger(SiChuanRenShiWorker.class);

    @Override
    public void run() {

        SiChuanRenShiDownloaderPool downloaderPool = SiChuanRenShiDownloaderPool.getInstance();
        FileWriterPool writerPool = FileWriterPool.getInstance();
        while (true && !Thread.interrupted()) {
            SiChuanRenShiDownloader downloader = null;
            FileWriter writer = null;
            try {
                downloader = downloaderPool.fetchDownLoader(1000);
                if(downloader != null) {
                    SiChuanRenShiTask task = (SiChuanRenShiTask) CommonTaskManager.takeTask("SiChuanRenShi",1000);
                    if(task == null) {
                        continue;
                    }
                    JSONObject content = downloader.crawlItem(task.getUrl());
                    JSONObject data = content.getJSONObject("data");
                    if(null != data ) {
                        writer = writerPool.fetchWriter();
                        //文件前缀
                        String filePrefix = "SiChuanRenShi";
                        String time = data.getString("publish_time");
                        long timeStamp = DateUtils.dateToTimestamp(DateUtils.parseDateTime(time)).getTime();
                        //平台名称（或者英文）_ 新闻发布的时间戳 _ urlMD5码
                        String fileName = filePrefix + "_" + timeStamp + "_" + MD5Utils.getHash3(data.getString("url").toString().replaceAll("#.*", ""), "MD5");
                        String publishDate = DateUtils.formatDate(DateUtils.parseDate(time)).replace("-", "-");
                        //获取文件的map
                        Map<String,File> map = downloader.getFiles();
                        writer.write("四川人事网_speeches_" + publishDate, fileName, JSON.toJSONString(content, SerializerFeature.BrowserCompatible), map);
                        //System.out.println(data.toJSONString());
                    }
                }
            } catch (InterruptedException e) {
                //爬虫出现异常则继续进行下一个任务
                e.printStackTrace();
                logger.error(e);
                continue;
            } catch (Exception e) {
                //爬虫出现异常则继续进行下一个任务
                e.printStackTrace();
                logger.error(e);
                continue;
            } finally {
                if(downloader != null) {
                    downloaderPool.releaseDownLoader(downloader);
                }
                if(writer != null) {
                    writerPool.releaseWriter(writer);
                }
            }


        }
    }
}
